Page({
  data: {
    url:'',
    list:[],
    flag:true,//是否可以点击按钮
    progress:0,//下载进度
    is_contact:false,
  },
  onLoad: function (options) {
    this.setData({
      list:wx.getStorageSync('xieyiImageArr')
    })
    if(options.url){
      this.setData({
        url:options.url
      })
    }
    if(options.is_contact){
      this.setData({
        is_contact:true
      })
    }
  },
  bottomTaps(){
    if(!this.data.flag){
      return
    }
    if(this.data.progress==100){
      wx.openDocument({
        filePath: this.data.filePath,
        success: function (res) {
          console.log('打开文档成功')
        }
      })
      return
    }
    wx.showModal({
      content:'是否下载附件',
      success:r=>{
        if (r.confirm) {
          this.setData({
            flag:false
          })
          const downloadTask = wx.downloadFile({
            url: this.data.url,
            success:  (res)=> {
              const filePath = res.tempFilePath
              this.setData({
                filePath:res.tempFilePath
              })
              wx.openDocument({
                filePath: filePath,
                success: function (res) {
                  console.log('打开文档成功')
                }
              })
            }
          })
          downloadTask.onProgressUpdate((res) => {
            if(res.progress==100){
              this.setData({
                progress:res.progress,
                flag:true
              })
            }
          })
        } else if (r.cancel) {

        }
      }
    })
  },
  click_tap(){
    if(this.data.is_contact){
      getApp().contact()
    }
  },
  onUnload() {
    wx.removeStorageSync('xieyiImageArr')
  },
})